// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ComVisionHTTPGateway.java

package cn.sendsms;

import java.io.IOException;
import java.net.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

// Referenced classes of package cn.sendsms:
//			HTTPGateway, TimeoutException, GatewayException, OutboundMessage, 
//			MessageStatuses, FailureCauses, DeliveryStatuses

public class ComVisionHTTPGateway extends HTTPGateway
{

	private String username;
	private String password;
	private Object sync;

	public ComVisionHTTPGateway(String id, String username, String password, Logger logger)
	{
		super(id, logger);
		this.username = username;
		this.password = calculateMD5(password);
		attributes = 133;
		sync = new Object();
	}

	boolean sendMessage(OutboundMessage msg)
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		URL url = null;
		boolean ok = false;
		try
		{
			String file = "/send.do?username=" + username + "&password=" + password + "&to=" + msg.getRecipient().substring(1) + "&message=" + URLEncoder.encode(msg.getText(), "ISO-8859-1");
			if (msg.getFrom() != null)
				file.concat("&from=" + URLEncoder.encode(msg.getFrom(), "ISO-8859-1"));
			url = new URL("http", "api.smsapi.pl", file);
			List response;
			synchronized (sync)
			{
				response = HttpGet(url);
			}
			String first = (String)response.get(0);
			Matcher m = Pattern.compile("(OK|ERROR):(\\d+)(:(\\d+))?").matcher(first);
			if (m.matches())
			{
				String stat = m.group(1);
				if (stat.equals("OK"))
				{
					msg.setRefNo(m.group(2));
					msg.setMessageStatus(MessageStatuses.SENT);
					ok = true;
				} else
				if (stat.equals("ERROR"))
				{
					FailureCauses c = FailureCauses.UNKNOWN;
					int err = Integer.parseInt(m.group(2));
					switch (err)
					{
					case 11: // '\013'
					case 12: // '\f'
					case 14: // '\016'
					case 300: 
						c = FailureCauses.BAD_FORMAT;
						break;

					case 13: // '\r'
						c = FailureCauses.BAD_NUMBER;
						break;

					case 15: // '\017'
					case 16: // '\020'
					case 101: // 'e'
					case 102: // 'f'
						c = FailureCauses.GATEWAY_AUTH;
						break;

					case 103: // 'g'
						c = FailureCauses.NO_CREDIT;
						break;

					case 200: 
						c = FailureCauses.GATEWAY_FAILURE;
						break;
					}
					msg.setFailureCause(c);
					msg.setMessageStatus(MessageStatuses.FAILED);
					ok = false;
				}
			} else
			{
				logError("Invalid response from provider");
			}
		}
		catch (MalformedURLException e)
		{
			logError("Malformed URL " + url);
		}
		catch (IOException e)
		{
			logError("I/O Error");
		}
		return ok;
	}

	public float queryBalance()
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		URL url = null;
		String file = "/send.do?username=" + username + "&password=" + password + "&points=1";
		url = new URL("http", "api.smsapi.pl", file);
		List response = HttpGet(url);
		String first = (String)response.get(0);
		Matcher m = Pattern.compile("(Points|ERROR): ?(\\d+)").matcher(first);
		if (m.matches())
		{
			String stat = m.group(1);
			if (stat.equals("Points"))
				return Float.parseFloat(m.group(2));
			else
				return -1F;
		} else
		{
			return -1F;
		}
	}

	public DeliveryStatuses queryMessage(String refNo)
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		DeliveryStatuses del = DeliveryStatuses.UNKNOWN;
		URL url = null;
		String file = "/send.do?username=" + username + "&password=" + password + "&status=" + refNo;
		url = new URL("http", "api.smsapi.pl", file);
		List response = HttpGet(url);
		String first = (String)response.get(0);
		Matcher m = Pattern.compile("(OK|ERROR):(\\d+)").matcher(first);
		if (m.matches())
		{
			String stat = m.group(1);
			if (stat.equals("OK"))
			{
				int code = Integer.parseInt(m.group(2));
				switch (code)
				{
				case 405: 
				case 406: 
					del = DeliveryStatuses.ABORTED;
					break;

				case 404: 
					del = DeliveryStatuses.DELIVERED;
					break;

				case 402: 
				case 403: 
					del = DeliveryStatuses.KEEPTRYING;
					break;

				case 401: 
					del = DeliveryStatuses.UNKNOWN;
					break;
				}
			}
		}
		return del;
	}
}
